package Model;


import java.io.Serializable;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Tran Thuy Duong
 */
import java.sql.*;
public class M_Login implements Serializable{
    
    private Connection conn;
    public void connect(){
         try {
            String userName = "root"; // root là mặc định của MySQL.
            String passWord = "";
            String url = 
            "jdbc:mysql://localhost:3306/handmade?useUnicode=true&characterEncoding=UTF-8"; 
           
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, userName, passWord);
            }
            catch (Exception e) {
                System.out.println("Diver sai");
            }
        
    }
    
    public M_Login(){}
    
    public boolean checkLogin(String username, String password){
        
        java.sql.Statement stmt = null;
        ResultSet rs = null;
        boolean result = false;
        //pasword khi cấu hình MySQL. 3306 là port của MySQL
       
        connect();
        try {
           
            String sql = "SELECT * FROM taikhoan WHERE TenDN = '"+username+"' and MatKhau='" + password + "'";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            
        
            boolean result_1 = rs.next();
            stmt.close();
            rs.close();
            conn.close();
            if (result_1) {
                result = true;
            }
            
            /*String str=null;
            
            if(rs.next())
            {
                 str = rs.getString("TenDN");
            }
             if(str.compareTo(username)==0)
                result = true;
             else result = false;*/
            
            
        } catch (SQLException s) {
           result = false;
        } 
            
            
       return result;

     
    }
    
}

